package com.shitou.shitoumember.service.impl;

import com.shitou.shitoucommons.utils.RespBean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.shitou.shitoucommons.utils.PageUtils;
import com.shitou.shitoucommons.utils.Query;

import com.shitou.shitoumember.dao.SendReceiveDao;
import com.shitou.shitoumember.entity.SendReceiveEntity;
import com.shitou.shitoumember.service.SendReceiveService;

@Transactional
@Service("sendReceiveService")
public class SendReceiveServiceImpl extends ServiceImpl<SendReceiveDao, SendReceiveEntity> implements SendReceiveService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SendReceiveEntity> page = this.page(
                new Query<SendReceiveEntity>().getPage(params),
                new QueryWrapper<SendReceiveEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 创建聊天关联表
     * @param memberId
     * @param beingMemberId
     * @return
     */
    @Override
    public RespBean createSendReceive(Long memberId, Long beingMemberId) {
        SendReceiveEntity sendReceive = this.getOne(new QueryWrapper<SendReceiveEntity>()
                .eq("member_id", memberId)
                .eq("being_member_id", beingMemberId));

        if ( sendReceive != null ){
            sendReceive.setStatus(1);
            this.updateById(sendReceive);
            return RespBean.success();
        }
        // 插入
        SendReceiveEntity sendReceiveEntity = new SendReceiveEntity();
        sendReceiveEntity.setCreateTime(new Date());
        sendReceiveEntity.setMemberId(memberId);
        sendReceiveEntity.setBeingMemberId(beingMemberId);
        sendReceiveEntity.setStatus(1);
        this.save(sendReceiveEntity);

        // 相互联系人，创建
        sendReceiveEntity.setMemberId(beingMemberId);
        sendReceiveEntity.setBeingMemberId(memberId);
        this.save(sendReceiveEntity);

        return RespBean.success();
    }


    /**
     * 删除聊天，不删除记录
     * @param memberId
     * @param beingMemberId
     * @return
     */
    @Override
    public RespBean deleteSendReceive(Long memberId, Long beingMemberId) {
        SendReceiveEntity sendReceive = this.getOne(new QueryWrapper<SendReceiveEntity>()
                .eq("member_id", memberId)
                .eq("being_member_id", beingMemberId));
        sendReceive.setStatus(0);
        this.updateById(sendReceive);

        return RespBean.success();
    }


    /**
     * 获取当前用户聊天的所有用户的id
     * @param memberId
     * @return
     */
    @Override
    public RespBean getMemberReceive(Long memberId) {
        List<SendReceiveEntity> list = this.list(new QueryWrapper<SendReceiveEntity>()
                .eq("member_id", memberId)
                .eq("status", 1));
        List<Long> idS = new ArrayList<>();
        for (SendReceiveEntity sendReceiveEntity : list) {
            idS.add(sendReceiveEntity.getBeingMemberId());
        }
        return RespBean.success().put("data", idS);
    }



}
